package com.study.zouchao.superproject.register;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.view.View;
import android.widget.EditText;
import android.widget.RelativeLayout;

import com.study.zouchao.superproject.R;
import com.study.zouchao.superproject.base_zou.MyBaseActivity;
import com.study.zouchao.superproject.base_zou.MyBaseFragment;
import com.study.zouchao.superproject.login.model.entity.User;
import com.study.zouchao.superproject.login.view.activity.LoginActivity;
import com.study.zouchao.superproject.util.App;
import com.study.zouchao.superproject.util.ToastUtils;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Created by Administrator on 2017/6/24.
 */

public class RegisterFragment extends MyBaseFragment implements IRegisterContract.IRegisterView {

    @BindView(R.id.id_fab_regiser)
    FloatingActionButton mFabRegister;

    @BindView(R.id.id_edit_username)
    EditText mEtUsername;

    @BindView(R.id.id_et_nickname)
    EditText mEtNickName;

    @BindView(R.id.id_edit_password)
    EditText mEtPwd;

    @BindView(R.id.id_et_ensure_pwd)
    EditText mEtEnsurePwd;

    @BindView(R.id.activity_register)
    RelativeLayout mLlRegister;

    private RegisterPresenter mPresenter;

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        init();
    }

    private void init() {
        mPresenter = new RegisterPresenter(getActivity(), this);
    }


    @Override
    protected View getLoadingTargetView() {
        return getCurrentFragmentRootView().findViewById(R.id.id_loading_register);
    }

    @Override
    protected int getFragmentLayoutID() {
        return R.layout.fragment_register;
    }

    @Override
    public void gotoLoginActivity() {
        if (getActivity() != null)
            getActivity().finish();
//        LoginActivity.actionLoginActivityByTop(getActivity(), null, null);
    }

    @Override
    public void showBaseLoading(boolean isShow) {
        if (isShow) showLoading("正在加载...");
        else hideLoading();
    }


    @Override
    public void finishCurrentRegisterActivity() {
        if (getActivity() != null)
            getActivity().finish();
    }

    @Override
    public void showUsernameError(String msg) {
        //TODO:妈的什么情况 setError不能使用
//        mEtUsername.setError(msg);

//        ToastUtils.showShort(App.getAppContext(), msg);
        showSnackBar("Warn！！"+msg, Snackbar.LENGTH_SHORT);

        mEtUsername.requestFocus();
    }

    @Override
    public void showNicknameError(String msg) {
//        mEtNickName.setError(msg);
//        ToastUtils.showShort(App.getAppContext(), msg);
        showSnackBar("Warn！！"+msg, Snackbar.LENGTH_SHORT);

        mEtNickName.requestFocus();
    }

    @Override
    public void showPwdError(String msg) {
//        mEtPwd.setError(msg);
//        ToastUtils.showShort(App.getAppContext(), msg);
        showSnackBar("Warn！！"+msg, Snackbar.LENGTH_SHORT);

        mEtPwd.requestFocus();
    }

    @Override
    public void showEnsurePwdError(String msg) {
//        mEtEnsurePwd.setError(msg);
        showSnackBar("Warn！！"+msg, Snackbar.LENGTH_SHORT);
//        ToastUtils.showShort(App.getAppContext(), msg);

        mEtEnsurePwd.requestFocus();
    }

    @Override
    public void initInfos(User user) {

    }

    @Override
    public void showSnackBar(String title, int showLength) {
        Snackbar.make(mLlRegister, title, showLength)
                .show();
    }

    @OnClick({R.id.id_fab_regiser})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.id_fab_regiser :
                clickAttemptRegister();
                break;
        }
    }
    private void clickAttemptRegister() {
        String username = mEtUsername.getText().toString().trim();
        String nickname = mEtNickName.getText().toString().trim();
        String pwd = mEtPwd.getText().toString().trim();
        String ensurePwd = mEtEnsurePwd.getText().toString().trim();

        mPresenter.confirmRegister(username, nickname, pwd, ensurePwd);
    }

}
